<template>
	<view>
		<u--form labelPosition="top" :model="userInfo" labelWidth="auto" ref="userInfo" :rules="rules">
			<u-form-item label="用户名" prop="member_name">
				<u--input v-model="userInfo.member_name" prefixIcon="../../static/img/login/user.svg"
					placeholder="请输入您的账户"></u--input>
			</u-form-item>
			<u-form-item label="设置密码" prop="member_passwd">
				<u--input v-model="userInfo.member_passwd" prefixIcon="../../static/img/login/password.svg"
					placeholder="请输入您的密码" :password="isPassword">
					<template slot="suffix">
						<u-icon name="eye-fill" @click="canSeeButton" v-if="!canSee"></u-icon>
						<u-icon name="eye-off" @click="canSeeButton" v-else></u-icon>
					</template>
				</u--input>
			</u-form-item>
			<u-form-item label="确认密码" prop="remember_passwd">
				<u--input v-model="userInfo.remember_passwd" prefixIcon="../../static/img/login/password.svg"
					placeholder="请再次输入您的密码" :password="isPasswordOne">
					<template slot="suffix">
						<u-icon name="eye-fill" @click="canSeeButtonOne" v-if="!canSeeOne"></u-icon>
						<u-icon name="eye-off" @click="canSeeButtonOne" v-else></u-icon>
					</template></u--input></u--input>
			</u-form-item>
			<u-form-item label="手机号码" prop="member_mobile">
				<u--input v-model="userInfo.member_mobile" prefixIcon="../../static/img/login/phone.svg"
					placeholder="请输入您的手机号码" type="number"></u--input>
			</u-form-item>
			<u-form-item label="短信验证" prop="code">
				<u-input placeholder="请输入您的验证码" prefixIcon="../../static/img/login/code.svg" v-model="userInfo.code">
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"
							start-text="获取"></u-code>
						<u-button @tap="getCode" :text="tips" size="mini" class="getcode_class"></u-button>
					</template>
				</u-input>
			</u-form-item>
		</u--form>
		<view class="">
			<u-button type="error" class="btn_class" @click="regesterButton(userInfo)">确认注册</u-button>
		</view>
		<u-toast ref="regesterRef"></u-toast>
	</view>
</template>

<script>
	import {
		regester
	} from '@/api/wei/index.js'
	export default {
		data() {
			return {
				userInfo: {
					member_name: '',
					member_passwd: '',
					remember_passwd: '',
					member_mobile: '',
					code: ''
				},
				tips: '',
				value: '',
				canSee: true,
				isPassword: true,
				canSeeOne: true,
				isPasswordOne: true,
				rules: {
					member_name: [{
						required: true,
						message: '请输入账户号码',
						trigger: ['blur', 'change']
					}],
					member_passwd: [{
						required: true,
						message: '请输入设置密码',
						trigger: ['blur', 'change']
					}],
					remember_passwd: [{
						required: true,
						message: '请输入确认密码',
						trigger: ['blur', 'change']
					}],
					// 字段名称
					member_mobile: [{
							required: true,
							message: '请输入手机号',
							trigger: ['blur', 'change'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['blur', 'change'],
						}
					],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					}],
				}
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			canSeeButton() {
				// console.log(132)
				this.canSee = !this.canSee,
					this.isPassword = !this.isPassword
			},
			canSeeButtonOne() {
				// console.log(132)
				this.canSeeOne = !this.canSeeOne,
					this.isPasswordOne = !this.isPasswordOne
			},
			regesterButton(userInfo) {
				this.$refs.userInfo.validate().then(res => {
					regester(userInfo).then(result => {
						console.log(result)
						if (result.data.code === 200) {
							this.$refs.regesterRef.show({
								message: result.data.message
							})
						} else {
							this.$refs.regesterRef.show({
								message: result.data.message
							})
						}
					})
				}).catch(errors => {
					uni.$u.toast('请按照规则填写信息')
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import url('../../static/css/login.css');
</style>